package com.hucc.redislock.command;

import com.hucc.redislock.RedisLockContext;
import com.hucc.redislock.result.CommandResult;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPubSub;

import java.util.ArrayList;
import java.util.List;

/**
 * 订阅锁释放消息
 */
public class SubscribeLockReleaseRedisCommand extends AbstractRedisCommand {
    private JedisPubSub jedisPubSub;

    public SubscribeLockReleaseRedisCommand(JedisPubSub jedisPubSub, String lockName) {
        super(lockName, null);
        this.jedisPubSub = jedisPubSub;
    }

    @Override
    public CommandResult execute() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                RedisLockContext.getJedis().subscribe(jedisPubSub, getChannelName(lockName));
            }
        }).start();

        CommandResult commandResult = new CommandResult();
        commandResult.setSuccess(true);
        return commandResult;
    }
}
